package com.dftdla.server.handler;

import com.dftdla.dao.mapper.UserGroupMapper;
import com.dftdla.result.ResponseResult;
import com.dftdla.server.message.request.GroupQuitRequestMessage;
import com.dftdla.util.Code;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 14501
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {

    @Resource
    private UserGroupMapper userGroupMapper;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) {
        int del = userGroupMapper.outGroup(msg.getUserId(),msg.getGroupId());
        String res;
        if(del > 0){
            res = new ResponseResult<>(Code.SUCCESS).toString();
        }else{
            res = new ResponseResult<>(Code.BAD_REQUEST.code,"用户不在该群组中").toString();
        }
        ctx.writeAndFlush(new TextWebSocketFrame(res));
    }
}
